<?php
/**
 * @version		$Id: manager.php 10381 2008-06-01 03:35:53Z yopensource $
 * @package		Feeds manager
 * @subpackage	Link Manager
 * @license		commercial
 * @author 		yopensource
 * @email		yopensource@gmail.com
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'phpWebHacks.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'find_replace.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'resource_url.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'autocuttext.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'tidy_clean.php');

jimport('joomla.application.component.model');
// Set the table directory
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

/**
 * Weblinks Component Weblink Model
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class FVBModelFeeds extends JModel
{
	/**
	 * amMap data array
	 *
	 * @var array
	 */
	var $_data = null;	

	/**
	 * uri total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	var $_feedEdit=null;
	
	var $errRemove=null;
	/**
	 * Constructor
	 *
	 * @since 0.9
	 */
	
	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;
		
		//$context			= 'com_content.viewcontent';
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'.feeds_manager.filter_order', 		'filter_order', 	'id',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.feeds_manager.filter_order_Dir',	'filter_order_Dir',	'',				'word' );		
		$search 			= $mainframe->getUserStateFromRequest( $option.'.feeds_manager.search', 			'search', 			'',				'string' );
		$search 			= JString::strtolower( $search );		
		
		$limit				= $mainframe->getUserStateFromRequest($option.'.feeds_manager.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart			= $mainframe->getUserStateFromRequest($option.'.feeds_manager.limitstart', 'limitstart', 0, 'int');
		
		$this->setState('limit', 		$limit);
		$this->setState('limitstart', 	$limitstart);
		$this->setState('search', 		$search);
		// Get the filter request variables
		$this->setState('filter_order', 	$filter_order);
		$this->setState('filter_order_dir', $filter_order_Dir);
	}
	
	function getTotal()
	{
		// Lets load the total nr if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery(); 
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}
	
	function getPagination()
	{		
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
	
		return $this->_pagination;
	}
	
	
	function getData(){
		$pop	= JRequest::getBool('pop');
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();

			if ($pop) {
				$this->_data = $this->_getList( $query );
			} else {
				$this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
			}
		}
	
		return $this->_data;
	}
	
	function _buildQuery(){
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildWhere();
		$orderby	= $this->_buildOrderBy();
		
		$config 	= &JComponentHelper::getParams('com_feed_manager');
		$data_type 	= $config->get('data_type');	
		
			//Get Events from Database
		$query = ' SELECT feeds.*
					FROM #__feeds AS feeds '.	$where. $orderby;
		return $query;
	}
	
	function _buildWhere( )
	{		
		
		$db					= &JFactory::getDBO();		
		$search				= $this->getState('search');	
		$where 				= array();
		
		if ( $search ) {
			$w 		= array();
			$w[] 	= 'feeds.title LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$w[] 	= 'feeds.link LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$w 		= implode( ' OR ', $w );
			$where[]=	'('.$w.')';
		}	
			
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
					
		return $where;
	}
	
	function _buildOrderBy()
	{
		$filter_order		=	$this->getState('filter_order');
		$filter_order_Dir	=	$this->getState('filter_order_dir');
		
		if ($filter_order == 'id'){
			$orderby 	= ' ORDER BY id';
		} else {
			$orderby 	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', id';
		}
		
		return $orderby;
	}
	
	function getLists(){
		
		// state filter
		$lists['state'] = JHTML::_('grid.state', $this->getState('filter_state'), 'Published', 'Unpublished');
		
		// table ordering
		$lists['order_Dir']	= $this->getState('filter_order_dir');
		$lists['order']		= $this->getState('filter_order');
		
		// search filter
		$lists['search']= $this->getState('search');
		
		$filter		= null;
		$filter_sectionid		=	$this->getState('sectionid');
		$catid					=	$this->getState('catid');
		
		$config 	= &JComponentHelper::getParams('com_feed_manager');
		$data_type 	= $config->get('data_type');	
	
		return $lists;
	}
	
	function filterCategory($query, $active = NULL)
	{
		// Initialize variables
		$db	= & JFactory::getDBO();

		$categories[] = JHTML::_('select.option', '0', '- '.JText::_('Select Category').' -');
		$db->setQuery($query);
		$categories = array_merge($categories, $db->loadObjectList());

		$category = JHTML::_('select.genericlist',  $categories, 'catid', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $active);

		return $category;
	}
	
/* FOR TEST	*/
	function get_document_type()
	{
		$site_url = JRequest::getVar('url');
		$site_url = base64_decode($site_url);
		$browser = new phpWebHacks();
		$header = @$browser->head($site_url);
		
//		if(!$header || $header['Status']['Code'] != '200'){
		if(!isset($header['Content-Type'])){
			return '{"type":"false"}';			
		}
		if (preg_match('/xml/i', $header['Content-Type'])) {
			return '{"type":"xml"}';	
		}
		return '{"type":"http"}';
	}
	
	function step2_test()
	{
		$browser = new phpWebHacks();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		
		$arr_links = array();
		
		if ($obj_json->document_type == 'xml') {			
			$options['rssUrl']		= $obj_json->link;
			$options['cache_time']	= 60;
			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			
			if (!$rssDoc)	return	'{"result":"false", "err_msg":"unable to read xml file"}';			
			
			$items	=	$rssDoc->get_items();
			$items	=	array_slice($items , 0, 10);

			foreach ($items as $item) {
				$arr_links[] = $item->get_link();
			}	
		}
		else {
			//http
			$response = @$browser->get($obj_json->link);

			if (!$response) {
				return '{"result":"false", "err_msg":"unable to connect"}';				
			}
			$obj_json->link = $browser->get_addressbar();
			if (!preg_match_all($obj_json->getlink_reg, $response, $matches)) {
				return '{"result":"false", "err_msg":"Invalid regex"}';				
			}
			$arr_links = $matches[$obj_json->getlink_sub];
		}
		
		for ($i = 0; $i < count($arr_links); $i++){
			$resource_url = new FVB_Resource_Url($arr_links[$i], $obj_json->link);
			//Process link urls (href)
			$arr_links[$i] = $resource_url->process_single_url();
		}
				
		//find and replace here
		if($obj_json->find_replace_links){
			for ($i = 0; $i < count($arr_links);$i++){
				$arr_links[$i] = YOS_Find_Replace::find_replace($arr_links[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_links));
			}
		}		
		$str_links = implode('|', $arr_links);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->links = $str_links;
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	
	function step2_test_link()
	{
		$browser = new phpWebHacks();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		if (!isset($obj_json->index))	return '{"result":"false", "err_msg":"invalid index"}';
				
		if (!$browser->head($obj_json->link)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"fail to connect"}';
		}
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->link = $browser->get_addressbar();
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	
	function step3_test()
	{
		$FVB_utility = new FVB_utility();
				
		$json = JRequest::getString('json', null, null, 2);
		if (!$json) 	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';		
		
		$arr_titles = array();
		$charset = false;
		if ($obj_json->document_type == 'xml') {
			//xml
			$options['rssUrl']		= $obj_json->link;
			$options['cache_time']	= 60;
			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			
			if (!$rssDoc) return '{"result":"false", "err_msg":"unable to read xml file"}';			
			
			$items	=	$rssDoc->get_items();
			$items	=	array_slice($items , 0, 10);

			foreach ($items as $item) {
				$arr_titles[] = $item->get_title();
			}			
		}
		else {
			//http
			$browser = new phpWebHacks();
			$response = @$browser->get($obj_json->link);
						
			if (!$response)	return '{"result":"false", "err_msg":"unable to connect"}';	
			
			$charset = $browser->get_charset();
				
			if (!preg_match_all($obj_json->gettitle_reg, $response, $matches))	return '{"result":"false", "err_msg":"Invalid regex"}';			
			
			$arr_titles = $matches[$obj_json->gettitle_sub];
		}
				
		for ($i = 0; $i < count($arr_titles); $i++){
			$arr_titles[$i] = $FVB_utility->charset_process($arr_titles[$i], $charset);
		}
						
		//Find and replace here
		if($obj_json->find_replace_titles){
			for ($i = 0; $i < count($arr_titles);$i++){
				$arr_titles[$i] = FVB_Find_Replace::find_replace($arr_titles[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_titles));
			}			
		}
				
		$str_titles = implode('|', $arr_titles);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->titles = $str_titles;
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	
	function step4_test()
	{
		$browser = new phpWebHacks();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		
		$arr_dates = array();
		
		if ($obj_json->document_type == 'xml') {			
			$options['rssUrl']		= $obj_json->link;
			$options['cache_time']	= 60;
			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			
			if (!$rssDoc)	return	'{"result":"false", "err_msg":"unable to read xml file"}';			
			
			$items	=	$rssDoc->get_items();
			$items	=	array_slice($items , 0, 10);

			foreach ($items as $item) {
				$arr_dates[] = $item->get_date();
			}	
		}
		else {
			
		}
				
		//find and replace here
		if($obj_json->find_replace_dates){
			for ($i = 0; $i < count($arr_dates);$i++){
				$arr_links[$i] = FVB_Find_Replace::find_replace($arr_dates[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_dates));
			}
		}		
		$str_dates = implode('|YOS_SEPARATOR|', $arr_dates);

		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->dates = $str_dates;
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	
	function step4_test_a_link()
	{
		
		
	}
	
	
	function step5_test()
	{
		$FVB_utility = new FVB_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (@!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		
		if (!$obj_json->links)	return '{"result":"false", "err_msg":"invalid urls"}';
		
		$arr_links = explode('|', $obj_json->links);
		
		$arr_intros = array();
		$charset = false;
			
		if($obj_json->getDateAway == 0 || $obj_json->getDateAway == 1){
			$str_real_url = $obj_json->link;
			
			//connect to the link page then get intro
			if ($obj_json->document_type == 'xml') {
				//xml
				$options['rssUrl']		= $obj_json->link;
				$options['cache_time']	= 60;
				$rssDoc =& JFactory::getXMLparser('RSS', $options);
				
				if (!$rssDoc)	return '{"result":"false", "err_msg":"unable to read xml file"}';			
				
				$items	=	$rssDoc->get_items();
				$items	=	array_slice($items , 0, count($arr_links));
				
				foreach ($items as $item) {
					$arr_intros[] = $item->get_description();
				}
			}
			else {
				//http
				$browser = new phpWebHacks();
				$response = @$browser->get($obj_json->link);
				if (!$response)	return '{"result":"false", "err_msg":"unable to connect"}';
				
				$charset = $browser->get_charset();
				
				$str_real_url = $browser->get_addressbar();
				
				if (!preg_match_all($obj_json->getintro_reg, $response, $matches))	return '{"result":"false", "err_msg":"invalid regex"}';				
				
				$arr_intros = array_slice($matches[$obj_json->getintro_sub], 0, count($arr_links));
			}
			
			for ($i = 0; $i < count($arr_intros); $i++){
				$arr_intros[$i] = $FVB_utility->charset_process($arr_intros[$i], $charset);
				
				$resource_url = new FVB_Resource_Url($arr_intros[$i], $str_real_url);
				//TODO: Process link urls (href)
				$arr_intros[$i] = $resource_url->process_href();
				
				//TODO: Process image source urls (src)
				$arr_intros[$i] = $resource_url->process_src();
			}		
		}
		else {
			return '{"result":"false", "err_msg":"invalid dataway"}';
		}
		
		//Find and replace here 
		if($obj_json->find_replace_intro){
			for ($i = 0; $i < count($arr_intros);$i++){
				$arr_intros[$i] = FVB_Find_Replace::find_replace($arr_intros[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_intro));
			}
		}
		
		$str_intros = implode('|YOS_SEPARATOR|', $arr_intros);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->intros = $str_intros;
		$str_return = json_encode($obj_return);
		return $str_return;		
	}
	
	function step5_test_a_link()
	{
		$FVB_utility = new FVB_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		
		if (!$json)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (@!$obj_json->link)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url #2"}';
		
		if (!isset($obj_json->index))	return '{"result":"false", "index":"NaN", "err_msg":"invalid index"}';

		$intro = '';	
		
		$browser = new phpWebHacks();
		$response = @$browser->get($obj_json->link);
		
		if (!$response) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
		}
		$link = $browser->get_addressbar();
		$charset = $browser->get_charset();
	
		if (!preg_match($obj_json->getintro_reg, $response, $matches)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid intro regex"}';
		} 

		$intro = $matches[$obj_json->getintro_sub];
		
		$intro = $yos_utility->charset_process($intro, $charset);
		
		$resource_url = new FVB_Resource_Url($intro, $link);
		//TODO: Process link urls (href)
		$intro = $resource_url->process_href();
		
		//TODO: Process image source urls (src)
		$intro = $resource_url->process_src();
				
		//Find and replace here 
		if($obj_json->find_replace_intro){
			$intro = YOS_Find_Replace::find_replace($intro, explode('|YOS_SEPARATOR|', $obj_json->find_replace_intro));
		}
		
		if(($obj_json->dataway == 2 || $obj_json->dataway == 3) && $obj_json->cut_content && $obj_json->numberword > 0){
			//TODO: Cut text here
			$auto_cutter = new AutoCutText($intro, $obj_json->numberword);
			$intro = $auto_cutter->getIntro(). '<hr />The text below is the fulltext data<hr />' .$auto_cutter->getFulltext();
		}
						
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->intro = $intro;
		$str_return = json_encode($obj_return);
		return $str_return;		
	}
	
	function step6_test()
	{
		$FVB_utility = new FVB_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		
		if (!$json)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (@!$obj_json->link)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url #2"}';
		
		if (!isset($obj_json->index))	return '{"result":"false", "index":"NaN", "err_msg":"invalid index"}';
				
		$str_full = '';						
		
		$browser = new phpWebHacks();
		$response = @$browser->get($obj_json->link);
		
		if (!$response) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
		}
		$link = $browser->get_addressbar();
		$charset = $browser->get_charset();
		
		if (!preg_match($obj_json->getfull_reg, $response, $matches)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid fulltext regex"}';
		}

		$str_full = $matches[$obj_json->getfull_sub];
		
		//firstly call utf8 convert
		$str_full = $FVB_utility->charset_process($str_full, $charset);
				
		$resource_url = new FVB_Resource_Url($str_full, $link);
		//Process link urls (href)
		$str_full = $resource_url->process_href();
		
		//Process image source urls (src)
		$str_full = $resource_url->process_src();
		
		//Find and replace here 
		if($obj_json->find_replace_full){
			$str_full = FVB_Find_Replace::find_replace($str_full, explode('|YOS_SEPARATOR|', $obj_json->find_replace_full));
		}
						
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->full = $str_full;
		$obj_return->full_clean = mostidy_clean($str_full);
		$str_return = json_encode($obj_return);
		return $str_return;		
	}
	
	function step7_test()
	{
		$FVB_utility = new FVB_utility();
		$json = JRequest::getString('json', null, null, 2);		
		
		if (!$json) return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid submition data"}';
		$obj_json = json_decode($json);
		if (!$obj_json->link)	return  '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid url"}';
		
		if (!isset($obj_json->index))	return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid index"}';			

		$arr_src = array();
		$arr_title = array();
		
		$browser = new phpWebHacks();
		$response = @$browser->get($obj_json->link);
		$str_real_url = $browser->get_addressbar();
		$link = $browser->get_addressbar();
		$charset = $browser->get_charset();
		
		if (!preg_match($obj_json->getfull_reg, $response, $matches)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid fulltext regex"}';
		}
		$str_full = $matches[$obj_json->getfull_sub];
		
		//firstly call utf8 convert
		$str_full = $FVB_utility->charset_process($str_full, $charset);
				
		$resource_url = new FVB_Resource_Url($str_full, $link);
		//Process link urls (href)
		$str_full = $resource_url->process_href();
		
		//Process image source urls (src)
		$str_full = $resource_url->process_src();
		
		//Find and replace here 
		if($obj_json->find_replace_full){
			$str_full = FVB_Find_Replace::find_replace($str_full, explode('|YOS_SEPARATOR|', $obj_json->find_replace_full));
		}
		if (!$response)	return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
//$obj_json->getImage_reg='/<tr[^>]*>\s*<td[^>]*>.*?<img[^>]*src=("|\'|\s*)([^"\']+)("|\'|\s*)[^>]*>.*?<\/td>\s*<\/tr>\s*<tr[^>]*>\s*<td[^>]*class=("|\'|\s*)Image("|\'|\s*)[^>]*>(.*?)<\/td>\s*<\/tr>/ism';		
		if ($obj_json->getImage_reg) {
			//find all other pages
			if (!preg_match_all($obj_json->getImage_reg, $str_full, $matches))	return	'{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"not found"}';

			if ($obj_json->getISrc_sub)
			{
				$arr_src = $matches[$obj_json->getISrc_sub];
				for ($i = 0; $i < count($arr_src); $i++){
						$resource_url = new FVB_Resource_Url($arr_src[$i], $str_real_url);
						//Process link urls (href)
						$arr_src[$i] = $resource_url->process_single_url();
					}
				}
			if ($obj_json->getITitle_sub)
			{
				$arr_title = $matches[$obj_json->getITitle_sub];				
				for ($i = 0; $i < count($arr_title); $i++){
					if($obj_json->find_replace_images){
						$arr_title[$i] = FVB_Find_Replace::find_replace($arr_title[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_images));
					}
				}
			}
		}
		$str_src = implode('|YOS_SEPARATOR|', $arr_src);
		$str_title = implode('|YOS_SEPARATOR|', $arr_title);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->images = $str_src;
		if (count($arr_title))
			$obj_return->titles = $str_title;
		else {
			$obj_return->titles = false;
		}
		
		$str_return = json_encode($obj_return);
		return $str_return;	
	}

/*	*/
/*	*/
	
	function save()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db		 		= &JFactory::getDBO();
		$user			= &JFactory::getUser();
		$oldtitle 		= JRequest::getString( 'oldtitle', '', 'post' );
		$post			= JRequest::get( 'post' );

		$config 	= &JComponentHelper::getParams('com_feed_manager');
		$data_type 	= $config->get('data_type');
		
		$type			=	JRequest::getString('type',0);
		if(!$type)
		{
			$link=JRequest::getString('link', null, null, 2);
			
			$browser = new phpWebHacks();
			$header = @$browser->head($link);
			if(!$header || $header['Status']['Code'] != '200'){
				echo '{"type":"false"}';			
			}
			if (preg_match('/xml/i', $header['Content-Type'])) {
				$type	=	'xml';				
			}
			else {
				$type	=	'http';
			}		
		}			
		// Set the table directory
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
		$row =& JTable::getInstance('Feeds','Table');
		$id	=	JRequest::getInt('id');
		
		if ($id) {
			$row->load($id);
		}
		
		if (!$row->bind( $post )) {
			JError::raiseError(500, $row->getError() );
		}
		
		if ($type == 'xml') {
			$options['rssUrl']=JRequest::getString('link', null, null, 2);
			$options['cache_time']=3600;

			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			if ( $rssDoc == false ) {
				$msg = JText::_('Error: Feed not retrieved');
				$mainframe->redirect('index.php?option=com_feed_manager&task=linkmanager', $msg);
				return;
			}

			// channel header and link
			$row->title_original		= $rssDoc->get_title();
//			$row->link 		 			= $rssDoc->get_link();
			$row->description_original	= $rssDoc->get_description();			
		} else {			
			$row->link					=	JRequest::getString('link', null, null, 2);
			$row->title_original		= "";
			$row->description_original	= "";
		}
		$row->type						=	$type;
//		var_dump($row);
//		die;
		$row->getLink_reg				=	JRequest::getString('getlink_reg', null, null, 2);		
		$row->find_replace_links		=	JRequest::getString('find_replace_links', null, null, 2);
		$row->getTitle_reg				=	JRequest::getString('gettitle_reg', null, null, 2);		
		$row->find_replace_titles		=	JRequest::getString('find_replace_titles', null, null, 2);		
		$row->getIntro_reg				=	JRequest::getString('getintro_reg', null, null, 2);
		$row->find_replace_intros		=	JRequest::getString('find_replace_intros', null, null, 2);		
		$row->getFull_reg				=	JRequest::getString('getfull_reg', null, null, 2);		
		$row->getFull_sub				=	JRequest::getString('getfull_sub', null, null, 2);		
		$row->find_replace_fulls		=	JRequest::getString('find_replace_fulls', null, null, 2);	
		$row->getImage_reg				=	JRequest::getString('getImage_reg', null, null, 2);	
		$row->getISrc_sub				=	JRequest::getString('getISrc_sub', null, null, 2);	
		$row->getITitle_sub				=	JRequest::getString('getITitle_sub', null, null, 2);	
		$row->find_replace_images		=	JRequest::getString('find_replace_images', null, null, 2);	
		$row->numberStore				=	JRequest::getString('numberStore', null, null, 2);	
		$row->numberView				=	JRequest::getString('numberView', null, null, 2);	
		$row->last_update				=	'0000-00-00 00:00:00';	
		$row->checked_out				=	0;	
			

		if (!$row->store()) {
			JError::raiseError(500, $row->getError() );
		}
		$row->checkin();

		return $row;
	}
	// FOR EDIT Feed
/*	*/
	function getFeedEdit(){		
		if ($this->_feedEdit) {
			return $this->_feedEdit;			
		}
		global $mainframe;
		// Initialize variables
		$doTask 	= JRequest::getCmd('task',null);
		
		$db			=& JFactory::getDBO();
		$user 		=& JFactory::getUser();
		$uid		= $user->get('id');
		$option		= JRequest::getCmd( 'option' );
		$cid		= JRequest::getVar( 'cid', array(0), '', 'array' );

		JArrayHelper::toInteger($cid, array(0));
		
		$edit		=	(strtolower($doTask) == 'feed.edit') || ($cid[0]);
	
		$row =& JTable::getInstance('feeds','Table');
	
		// load the row from the db table
		if ($edit)
			$row->load( $cid[0] );		
		
		$this->_feedEdit=$row;		
		return $this->_feedEdit;
	}	
	function getListsEdit(){
		
		$db			=	&JFactory::getDBO();
		
		$cid		= JRequest::getVar( 'cid', array(0), '', 'array' );
	
		JArrayHelper::toInteger($cid, array(0));
		$doTask 	= JRequest::getCmd('task',null);
		$edit		=	strtolower($doTask) == 'link.edit';
		$sectionid	=	0;

		$row =$this->getFeedEdit();
		// load the row from the db table
		if ($edit)
			$row->load( $cid[0] );
		
		$config 	= &JComponentHelper::getParams('com_feed_manager');
		$data_type 	= $config->get('data_type');
		// build the html radio buttons for option readmore	
//		$cut_content = ($row->id) ? $row->cut_content : 0;	
//		$lists['cut_content'] 		= JHTML::_('select.booleanlist',  'cut_content', 'class="inputbox"', $cut_content );	
		// Select plug-in for chart
		$query = "SELECT id, name FROM #__plugins WHERE folder = 'feeds' AND published = 1";
		$db->setQuery($query);		
		$plugin = $db->loadObjectList();
		$html	= '<select name="pid" id="pid">';
		$html	.='<option value="0">------------------</option>';
		$html	.= JHTML::_('select.options', $plugin, 'id', 'name', $row->pid );
		$html	.= '</select>';
		$lists['plugin_id']			= $html;
		
		// select dataway
		$dataway	=	($row->id) ? $row->getDateAway : 0;
		$obj		= 	new JObject();
		$obj->value	=	0;
		$obj->text	=	'rss';
		$option[]	=	$obj;
		$obj		= 	new JObject();
		$obj->value	=	1;
		$obj->text	=	'detail';
		
		$option[]	=	$obj;
		$lists['getDateAway']		=	JHTML::_('select.genericlist', $option, 'getDateAway', 'class="inputbox" size="1"', 'value', 'text', $dataway);		
		return $lists;
	}
	function remove()
	{		
		// Initialize variables
		$db 	=& JFactory::getDBO();
		$cids 	= JRequest::getVar('cid', array(0), 'post', 'array');
		JArrayHelper::toInteger($cid);

		if (count( $cids ) < 1) {
			JError::raiseError(500, JText::_( 'Select a Feed to delete', true ));
		}
		$arr_err=array();
		$cid	=	implode(',',$cids);
		$query = 'DELETE FROM #__feeds'
		. ' WHERE id = ( '.$cid.' )'
		;
		$db->setQuery( $query );
		if (!$db->query()) {
			JError::raiseError( 500, $db->stderr() );
			return false;
		}
	
		$this->errRemove=$arr_err;
		if (count($arr_err)) {
			$this->errRemove=$arr_err;
		}
		return;
	}	
/*	*/
}
